package com.epro.dx.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.epro.dx.R;
import com.epro.dx.activity.currency.CurrencyActivity;
import com.epro.dx.task.GetVersionTask;
import com.epro.dx.util.ActivityUtil;
import com.epro.dx.util.IntentUtils;
import com.google.android.gms.analytics.HitBuilders;
import com.google.android.gms.analytics.Tracker;
import com.weipu.common.DXApplication;
import com.weipu.common.DXApplication.TrackerName;
import com.weipu.common.constants.Constant;
import com.weipu.common.constants.ServerConstant;
import com.weipu.common.util.StringUtil;

public class SettingsActivity extends BaseFragmentActivity implements
		OnClickListener {

	private Tracker tracker;
	private TextView currencyCode;
	private Button logout;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		 //集成GA
        tracker = ((DXApplication)getApplication()).getTracker(TrackerName.APP_TRACKER);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_settings);
		setTitleContent(R.string.function_bar_settings);
		setLeftBtnContent(true, -1);
		initView();
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		tracker.setScreenName("SettingsCenter");
		tracker.send(new HitBuilders.ScreenViewBuilder().build());
		//设置显示默认或选中的CurrencyCode
		currencyCode.setText(
				StringUtil.getConfig(
						ServerConstant.Parameter.PARAMETER_NAME_CURRENCYCODE,
						getString(R.string.currency_default_currencyCode)));
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		tracker.setScreenName(null);
	}

	private void initView() {
		RelativeLayout aboutUsItem = (RelativeLayout) findViewById(R.id.settings_aboutus_layout);
		RelativeLayout contactUsItem = (RelativeLayout) findViewById(R.id.settings_contactus_layout);
		RelativeLayout helpItem = (RelativeLayout) findViewById(R.id.settings_help_layout);
		RelativeLayout privacyItem = (RelativeLayout) findViewById(R.id.settings_privacy_layout);
		RelativeLayout conditionsItem = (RelativeLayout) findViewById(R.id.settings_conditions_layout);
		RelativeLayout currencyItem = (RelativeLayout) findViewById(R.id.settings_currency_layout);
		currencyCode = (TextView) findViewById(R.id.settings_currency_selected);
		logout = (Button) findViewById(R.id.logout_btn);
		logout.setOnClickListener(this);
		aboutUsItem.setOnClickListener(this);
		contactUsItem.setOnClickListener(this);
		helpItem.setOnClickListener(this);
		privacyItem.setOnClickListener(this);
		conditionsItem.setOnClickListener(this);
		currencyItem.setOnClickListener(this);
		// orderItem.setOnClickListener(this);
		// orderItem.setVisibility(View.GONE);
		
		if(ActivityUtil.isLogin(getApplicationContext())){
			//如果已经登陆，显示注销按钮
			logout.setVisibility(View.VISIBLE);
		}else{
			//没登陆，隐藏注销按钮
			logout.setVisibility(View.GONE);
		}
	}

	@Override
	public void onClick(View v) {
		Intent intent = new Intent();
		switch (v.getId()) {
		case R.id.settings_currency_layout:
			intent.setClass(this, CurrencyActivity.class);
			break;
		case R.id.settings_aboutus_layout:
//			intent.setClass(this, AboutUsActivity.class);
			getNewVersionCode();

			return;
		case R.id.settings_contactus_layout:
			intent.setClass(this, InfoChildActivity.class);
			intent.putExtra("settings_item", 2);
			break;
		case R.id.settings_help_layout:
			intent.setClass(this, InfoChildActivity.class);
			intent.putExtra("settings_item", 3);
			break;
		case R.id.settings_privacy_layout:
			intent.setClass(this, InfoChildActivity.class);
			intent.putExtra("settings_item", 4);
			break;
		case R.id.settings_conditions_layout:
			intent.setClass(this, InfoChildActivity.class);
			intent.putExtra("settings_item", 5);
			break;
		case R.id.logout_btn:
			new AlertDialog.Builder(SettingsActivity.this)
			.setTitle(R.string.logout)
			.setCancelable(true)
			.setMessage(R.string.sure_logout)
			.setPositiveButton(R.string.btn_yes,
					new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog,
						int which) {
					((BaseFragmentActivity)SettingsActivity.this).logout();
					Intent intent = new Intent();
					intent.setClass(SettingsActivity.this, HomeActivity.class);
					intent.putExtra(HomeActivity.CURRENT_TAB_TAG,
							getString(HomeActivity.titleArray[3]));
					finish();
				}
				
			})
			.setNegativeButton(R.string.btn_no,
					new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog,
						int which) {
					dialog.dismiss();
				}
			}).create().show();
			return;
		default:
			break;
		}
		startActivity(intent);
	}

	/**
	 * 从服务器上获取最新的版本号
	 */
	private void getNewVersionCode() {
		GetVersionTask getVersionTask = new GetVersionTask(this,new GetVersionTask.OnGetNewVersionCompletedListener() {
			@Override
			public void OnGetNewVersionCompleted(String newVersionCode) {
				Bundle param = new Bundle();
				param.putString(Constant.Intent.INTENT_PUT_VERSIONCODE,newVersionCode);
				IntentUtils.launchActivity(SettingsActivity.this,AboutUsActivity.class,param);
			}
		});
		getVersionTask.execute();
	}
}
